package bit.book.controller;

import bit.book.entity.UserInfo;
import bit.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(String username, String password, HttpSession session) {
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }


        UserInfo userInfo=userService.queryUserByName(username);
        if(userInfo==null) return false;
        if(password.equals(userInfo.getPassword())){
            session.setAttribute("user_session", userInfo);
            return true;
        }

//        if("admin".equals(username) &&"123" .equals(password)) {
//            session.setAttribute("username", username);
//            return true;
//        }
        return false;
    }
}
